﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Oracle.DataAccess.Client;
using System.Web.Configuration;
using System.Data;
using LCUV;

namespace sWarranty.WEB
{
    public partial class FindUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string updateParentScript =
                  @"function updateParentWindow(a)
                     {           
                       var fName= a; //document.getElementById('SelectedValue').value;     
                       var arrayValues= new Array(fName);
                       window.opener.updateValues(arrayValues);       
                       window.close(); 
                     }";
            this.ClientScript.RegisterStartupScript(this.GetType(), "UpdateParentWindow", updateParentScript, true);

            if (!IsPostBack)
                LoadDept();

        }

        protected void LoadDept()
        {
            String cnn = WebConfigurationManager.ConnectionStrings["tnsString"].ConnectionString;
            OracleConnection con = new OracleConnection(cnn);
            con.Open();
            OracleCommand cmd = con.CreateCommand();
            string query = "select * from VI_VN_CM_ORG_MST@dbl_mes where effective_end_date >= to_date(TO_DATE('" + DateTime.Now.Year + "-" + DateTime.Now.Month + "-" + DateTime.Now.Day  + "', 'yyyy-mm-dd')) order by name";
            cmd.CommandText = query;
            OracleDataAdapter a = new OracleDataAdapter(cmd);
            DataTable dt = new DataTable();
            a.Fill(dt);
            cmd.Dispose();

            ListItem i = new ListItem();
            i.Value = "0";
            i.Text = "-- Select --";
            dropPept.Items.Add(i);
            foreach (DataRow item in dt.Rows)
            {
                i = new ListItem();
                i.Value = item["ORGANIZATION_CODE"].ToString();
                i.Text = item["NAME"].ToString();
                dropPept.Items.Add(i);
            }
        }

        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                string query = "select * from VI_VN_CM_EMP_MST@dbl_mes where 1= 1 ";
                if (!string.IsNullOrEmpty(txtEmpNo.Text))
                    query += " and  EMPLOYEE_NUMBER like '%" + txtEmpNo.Text.ToUpper() + "%' ";
                if (!string.IsNullOrEmpty(txtEmail.Text))
                    query += " and  EMAIL_ADDRESS like '%" + txtEmail.Text + "%' ";
                if (!string.IsNullOrEmpty(txtName.Text))
                    query += " and  FULLNAME_ENGLISH like '%" + txtName.Text.ToUpper() + "%' ";
                if (dropPept.SelectedValue != "0")
                    query += " and ACC_ORG_CODE = '" + dropPept.SelectedValue + "' ";

                String cnn = WebConfigurationManager.ConnectionStrings["tnsString"].ConnectionString;
                OracleConnection con = new OracleConnection(cnn);
                con.Open();
                OracleCommand cmd = con.CreateCommand();
                cmd.CommandText = query;
                OracleDataAdapter a = new OracleDataAdapter(cmd);
                DataTable dt = new DataTable();
                a.Fill(dt);
                cmd.Dispose();
                litData.Text = "";

                if (dt.Rows.Count > 0)
                {
                    divData.Visible = true;
                    foreach (DataRow item in dt.Rows)
                    {
                        string email = item["EMAIL_ADDRESS"].ToString();
                        email = email.Substring(0, email.IndexOf("@")) + "|" + item["EMPLOYEE_NUMBER"].ToString() + "|" + item["FULLNAME_ENGLISH"].ToString() + "|" + item["LOCATION_NAME"].ToString();
                        //email|EMPNO|NAME|DEPT

                        litData.Text += "<tr>";
                        litData.Text += "   <td>" + item["EMPLOYEE_NUMBER"].ToString();
                        litData.Text += "   </td>";
                        litData.Text += "   <td>" + item["EMAIL_ADDRESS"].ToString();
                        litData.Text += "   </td>";
                        litData.Text += "   <td>" + item["FULLNAME_ENGLISH"].ToString();
                        litData.Text += "   </td>";
                        litData.Text += "   <td>" + item["LOCATION_NAME"].ToString();
                        litData.Text += "   </td>";
                        litData.Text += "   <td><input type='button' value='Select' id='btnSelect' onclick=\"updateParentWindow('" + email + "')\" class='button' />";
                        litData.Text += "   </td>";
                        litData.Text += "</tr>";
                    }
                }
                else
                {
                    divData.Visible = false;

                    ShowInfo("No User found");
                }
            }
            catch (Exception ee)
            {
                ShowError(ee);
            }
        }
        public void ShowError(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification error\"><div class=\"text\"><p><strong>Error!</strong> " + er + " </p></div></div>", false);
        }
        public void ShowError(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification error\"><div class=\"text\"><p><strong>Error!</strong> " + er + " </p></div></div>", false);
            }
        }
        public void ShowInfo(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification info\"><div class=\"text\"><p><strong>Information!</strong> " + er + " </p></div></div>", false);
        }
        public void ShowInfo(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification info\"><div class=\"text\"><p><strong>Information!</strong> " + er + " </p></div></div>", false);
            }
        }
        public void ShowWarning(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification warning\"><div class=\"text\"><p><strong>Warning!</strong> " + er + " </p></div></div>", false);
        }
        public void ShowWarning(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification warning\"><div class=\"text\"><p><strong>Warning!</strong> " + er + " </p></div></div>", false);
            }
        }
        public void ShowSuccess(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification success\"><div class=\"text\"><p><strong>Success!</strong> " + er + " </p></div></div>", false);
        }
        public void ShowSuccess(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification success\"><div class=\"text\"><p><strong>Success!</strong> " + er + " </p></div></div>", false);
            }
        }
        public void ShowTip(string er)
        {
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification tip\"><div class=\"text\"><p><strong>Tip!</strong> " + er + " </p></div></div>", false);
        }
        public void ShowTip(Exception ex)
        {
            if (ex.InnerException != null)
            {
                string er = ex.InnerException.Message + " at " + ex.TargetSite.Name + "()";
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "abcdefgh", "<div class=\"notification tip\"><div class=\"text\"><p><strong>Tip!</strong> " + er + " </p></div></div>", false);
            }
        }
    }
}